/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Grades_Add.java
 *
 * Created on Jan 15, 2013, 10:52:49 PM
 */
package Forms;

import java.util.ArrayList;
import javax.swing.JFrame;
import Classes.*;
import Classes.Event;
import java.awt.event.KeyEvent;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Owner
 */


public class AttendanceS_Manage extends javax.swing.JFrame {
    static User cUser;
    static Attendance ev;
    ArrayList<Object> list;
    ArrayList<Object> attendanceList1 = null;
    ArrayList<Object> attendanceList2 = null;
    ArrayList<Object> attendanceList3 = null;
    Registry r;
    static MInterface mi;
    String a5 = null; 
    String val = null;
    String port;
    _Login l = new _Login();
    String name;
    String end;
    String start;
    int start1;
    int  end1;
    int ans;
    int june;
    int july;
    int august;
    int september;
    int october;
    int november;
    int december;
    int january;
    int february;
    int march;
    String ab4 = "31";
    int a4 = 31;
    String userID;
    String usertID;
    int numOfDays;
    int val22;
    String sy;
    public AttendanceS_Manage(MInterface i, User a) {
        
        initComponents();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setLocationRelativeTo(getRootPane());
       mi = i;
       cUser = a;
        addButton.setVisible(false);
        editButton.setVisible(false);
        port = l.serverAdd;
        
        try {            
           userID = a.getID();
           usertID = a.getType();
            
            timeL.setText(mi.getCurrentTime());
            dateL.setText(mi.getCurrentDate());
                
             sy = null;
            //if (mi.getCurrentDate().compareTo((mi.get("get-endsy", mi.getCurrentDate(), "")) + "") > 0) {
                sy = mi.getToday() + "-" + mi.getNext();
            //} else if (mi.getCurrentDate().compareTo((mi.get("get-endsy", mi.getCurrentDate(), "")) + "") < 0) {
            //    sy = mi.getPast() + "-" + mi.getToday();              
            //}
           
            if(usertID.matches("Administrator")){
            editButton.setVisible(true);
            editButton.setEnabled(true);
            addButton.setVisible(false);
            addButton.setEnabled(false);
            list = mi.getList("attendanceManage");
            ArrayList<Attendance> ss = new ArrayList<Attendance>();
            for(int x = 0; x < list.size(); x++) {
                ss.add((Attendance) list.get(x));
                
            }

            DefaultTableModel dtm = new DefaultTableModel(new String[] {gaGradeTbl1.getColumnName(0), gaGradeTbl1.getColumnName(1), gaGradeTbl1.getColumnName(2), gaGradeTbl1.getColumnName(3), gaGradeTbl1.getColumnName(4)}, 0);
            gaGradeTbl1.setModel(dtm);
            for(int x= 0; x < ss.size(); x++) {
                dtm.insertRow(x, new Object[] {ss.get(x).getidno(),ss.get(x).getname(), ss.get(x).getclasslevel(), ss.get(x).getsection(),  ss.get(x).getsy()});
            
            }
            
            }else if(usertID.matches("Adviser")){
            editButton.setVisible(false);
            editButton.setEnabled(false);
            addButton.setVisible(true);
            addButton.setEnabled(true);
            list = mi.getList4("attendanceManage",userID);
            ArrayList<Attendance> ss = new ArrayList<Attendance>();
            for(int x = 0; x < list.size(); x++) {
               Attendance a1 = (Attendance) list.get(x);
               if(a1.getsection().equals(sy)){
                 ss.add((Attendance) list.get(x));
               }
              
                
            }

            DefaultTableModel dtm = new DefaultTableModel(new String[] {gaGradeTbl1.getColumnName(0), gaGradeTbl1.getColumnName(1), gaGradeTbl1.getColumnName(2), gaGradeTbl1.getColumnName(3), gaGradeTbl1.getColumnName(4)}, 0);
            gaGradeTbl1.setModel(dtm);
            for(int x= 0; x < ss.size(); x++) {
                dtm.insertRow(x, new Object[] {ss.get(x).getidno(),ss.get(x).getname(), ss.get(x).getclasslevel(), ss.get(x).getsection(),  ss.get(x).getsy()});
            
            }
            }
                              
           
        }catch(Exception e){
            System.out.println(e);
            e.printStackTrace();
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bannerL = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        addButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        totalFld = new javax.swing.JTextField();
        timeL = new javax.swing.JLabel();
        dateL = new javax.swing.JLabel();
        jScrollPane49 = new javax.swing.JScrollPane();
        gaGradeTbl1 = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex){
                return false;
            }
        };
        jScrollPane66 = new javax.swing.JScrollPane();
        gaAttTbl = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex){
                return false;
            }
        };
        jButton3 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Attendance Management");

        bannerL.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/banner.jpg"))); // NOI18N
        bannerL.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton6.setText("Search");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton6MouseClicked(evt);
            }
        });

        jTextField6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextField6.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField6KeyTyped(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setText("Logout");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Tardy:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Absent:");

        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Present:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Month:");

        jTextField4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTextField3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        addButton.setText("ADD");
        addButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addButtonMouseClicked(evt);
            }
        });

        editButton.setText("EDIT");
        editButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editButtonMouseClicked(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Total:");

        totalFld.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        totalFld.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(totalFld, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        timeL.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        timeL.setText("hh:mm am/pm");

        dateL.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        dateL.setText("mm/dd/yyyy");

        jScrollPane49.setBackground(new java.awt.Color(255, 255, 255));

        gaGradeTbl1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gaGradeTbl1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        gaGradeTbl1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id Number", "Name", "Section", "School Year", "Class Level"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        gaGradeTbl1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                gaGradeTbl1MouseClicked(evt);
            }
        });
        jScrollPane49.setViewportView(gaGradeTbl1);

        gaAttTbl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gaAttTbl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        gaAttTbl.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Month", "No. of Days Present", "No. of Days Absent", "No. of Days Tardy"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        gaAttTbl.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                gaAttTblMouseClicked(evt);
            }
        });
        jScrollPane66.setViewportView(gaAttTbl);

        jButton3.setText("Cancel");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton7.setText("Save");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bannerL, javax.swing.GroupLayout.PREFERRED_SIZE, 1276, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(dateL)
                            .addGap(31, 31, 31)
                            .addComponent(timeL)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton6))
                                .addComponent(jScrollPane49, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(10, 10, 10)
                            .addComponent(jScrollPane66, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton7)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton3))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bannerL, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(dateL)
                    .addComponent(timeL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(254, 254, 254))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 10, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane49, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(179, 179, 179))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane66)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField6KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField6KeyTyped
        if(evt.getKeyChar() == KeyEvent.VK_ENTER){
            search();
        }       
        
        String s = "[a-zA-Z0-9\\._\\-]{3,}";
        char c = evt.getKeyChar();
    
        if(c == KeyEvent.VK_SPACE) {
            if(jTextField6.getText().isEmpty()) {
                evt.consume();
                JOptionPane.showMessageDialog (null, "Space bar not allowed as first character.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }  
    }//GEN-LAST:event_jTextField6KeyTyped

    private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton6MouseClicked
        search();
    }//GEN-LAST:event_jButton6MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int x = JOptionPane.showConfirmDialog(null, "Are you sure you want to logout already?", "Alert!", JOptionPane.YES_NO_OPTION);
        if (x == JOptionPane.YES_OPTION) {
            try {
                mi.logout(cUser.getUsername());
                this.setVisible(false);
                new _Login().setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addButtonMouseClicked
        String a = jTextField1.getText().trim();
        String ab2 = jTextField2.getText().trim();
        String ab1 = jTextField3.getText().trim();
        String ab3 = jTextField4.getText().trim();

        int a2 = Integer.parseInt(ab1);
        int a1 = Integer.parseInt(ab2);
        int a3 = Integer.parseInt(ab3);

        String m = null;
        try {

            if (a1 + a2 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Present And Number of Absent is greater than numberofschooldays", "Error!!!", 0);
            } else if (a1 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Present is greater than numberofschooldays", "Error!!!", 0);
            } else if (a2 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Absent is greater than numberofschooldays", "Error!!!", 0);
            } else if (a1 + a2 != numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Absent and number of present is not equal to numberofschooldays", "Error!!!", 0);
            } else if (a3 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Tardy is greater than numberofschooldays", "Error!!!", 0);
            } else {
                m = mi.addAttendance(a, a1, a2, a3, numOfDays, a5);
                JOptionPane.showMessageDialog(null, "Attendance Successfully Added", "Attendance Added", 1);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                ref();
            }





        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error", "Error", 0);
        }
    }//GEN-LAST:event_addButtonMouseClicked

    private void editButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editButtonMouseClicked
        String a = jTextField1.getText();
        String ab2 = jTextField2.getText();
        String ab1 = jTextField3.getText();
        String ab3 = jTextField4.getText();
        //int a = Integer.parseInt(ab);
        int a2 = Integer.parseInt(ab1);
        int a1 = Integer.parseInt(ab2);
        int a3 = Integer.parseInt(ab3);


        String m = null;
        try {
            if (a1 + a2 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Present And Number of Absent is greater than numberofschooldays", "Error!!!", 0);
            } else if (a1 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Present is greater than numberofschooldays", "Error!!!", 0);
            } else if (a2 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Absent is greater than numberofschooldays", "Error!!!", 0);
            } else if (a1 + a2 != numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Absent and number of present is not equal to numberofschooldays", "Error!!!", 0);
            } else if (a3 > numOfDays) {
                JOptionPane.showMessageDialog(null, "Number of Tardy is greater than numberofschooldays", "Error!!!", 0);
            } else {
                m = mi.editAttendance(a, a1, a2, a3, numOfDays, a5);
                JOptionPane.showMessageDialog(null, "Attendance Successfully Edited", "Attendance Edited", 1);
                ref();
            }



        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error", "Error", 0);
            System.out.println(e);
        }

    }//GEN-LAST:event_editButtonMouseClicked

    private void gaGradeTbl1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gaGradeTbl1MouseClicked

        if (evt.getClickCount() == 2) {
            int x = gaGradeTbl1.getSelectedColumn();
            while (x != 0) {
                x--;
            }
            int y = gaGradeTbl1.getSelectedRow();
            val = (String) gaGradeTbl1.getValueAt(y, x);
            try {

                String data[][] = {{"January", " ", " ", " "},
                    {"February", " ", " ", " "},
                    {"March", " ", " ", " "},
                    {"June", " ", " ", " "},
                    {"July", " ", " ", " "},
                    {"August", " ", " ", " "},
                    {"September", " ", " ", " "},
                    {"October", " ", " ", " "},
                    {"November", " ", " ", " "},
                    {"December", " ", " ", " "},};


                attendanceList1 = mi.getList4("attendanceManage2", val);
                ArrayList<Attendance> uu1 = new ArrayList<Attendance>();
                for (int x1 = 0; x1 < attendanceList1.size(); x1++) {
                    uu1.add((Attendance) attendanceList1.get(x1));
                    a5 = uu1.get(x1).getstuID();

                }

                attendanceList2 = mi.getList4("attendanceManage3", val);
                ArrayList<Attendance> uu2 = new ArrayList<Attendance>();
                for (int x2 = 0; x2 < attendanceList2.size(); x2++) {
                    uu2.add((Attendance) attendanceList2.get(x2));
                    a5 = uu2.get(x2).getstuID();


                }

//                 attendanceList3 = mi.getList("attendanceManage4");
//                 ArrayList<Attendance> uu3 = new ArrayList<Attendance>();
//                 for(int x3 = 0; x3 < attendanceList3.size(); x3++) {
//                 uu3.add((Attendance) attendanceList3.get(x3));    
//            }


                DefaultTableModel dtm1 = new DefaultTableModel(data, new String[]{gaAttTbl.getColumnName(0), gaAttTbl.getColumnName(1), gaAttTbl.getColumnName(2), gaAttTbl.getColumnName(3)});
                gaAttTbl.setModel(dtm1);

                for (int x1 = 0; x1 < uu1.size(); x1++) {


                    if (uu1.get(x1).getmonth().equals("January")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 0, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 0, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 0, 3);
                    } else if (uu1.get(x1).getmonth().equals("February")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 1, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 1, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 1, 3);
                    } else if (uu1.get(x1).getmonth().equals("March")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 2, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 2, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 2, 3);
                    } else if (uu1.get(x1).getmonth().equals("June")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 3, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 3, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 3, 3);
                    } else if (uu1.get(x1).getmonth().equals("July")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 4, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 4, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 4, 3);
                    } else if (uu1.get(x1).getmonth().equals("August")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 5, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 5, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 5, 3);
                    } else if (uu1.get(x1).getmonth().equals("September")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 6, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 6, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 6, 3);
                    } else if (uu1.get(x1).getmonth().equals("October")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 7, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 7, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 7, 3);
                    } else if (uu1.get(x1).getmonth().equals("November")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 8, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 8, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 8, 3);
                    } else if (uu1.get(x1).getmonth().equals("December")) {
                        dtm1.setValueAt(uu1.get(x1).getpre(), 9, 2);
                        dtm1.setValueAt(uu1.get(x1).getabs(), 9, 1);
                        dtm1.setValueAt(uu1.get(x1).gettardy(), 9, 3);
                    }




                }


            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
                System.out.println(e);
            }


        }
    }//GEN-LAST:event_gaGradeTbl1MouseClicked

    private void gaAttTblMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gaAttTblMouseClicked
        if (evt.getClickCount() == 2) {
            int x = gaAttTbl.getSelectedColumn();
            while (x != 0) {
                x--;
            }
            int y = gaAttTbl.getSelectedRow();
            String val = (String) gaAttTbl.getValueAt(y, x);
            String val2 = (String) gaAttTbl.getValueAt(y, 1);
            String val3 = (String) gaAttTbl.getValueAt(y, 2);
            String val4 = (String) gaAttTbl.getValueAt(y, 3);

            try {

                jTextField1.setEnabled(false);
                jTextField1.setText(val.trim());
                jTextField2.setText(val2.trim());
                jTextField3.setText(val3.trim());
                jTextField4.setText(val4.trim());

                String as = "";
                String asd = " ";


                if (!val2.equals(as) && !val3.equals(as) && !val4.equals(as) && !val2.equals(asd) && !val3.equals(asd) && !val4.equals(asd) && !val2.isEmpty() && !val3.isEmpty() && !val4.isEmpty()) {

                    addButton.setVisible(false);
                } else {
                    addButton.setVisible(true);
                }

                if (val.matches("January")) {
                    val22 = 1;
                } else if (val.matches("February")) {
                    val22 = 2;
                } else if (val.matches("March")) {
                    val22 = 3;
                } else if (val.matches("June")) {
                    val22 = 6;
                } else if (val.matches("July")) {
                    val22 = 7;
                } else if (val.matches("August")) {
                    val22 = 8;
                } else if (val.matches("September")) {
                    val22 = 9;
                } else if (val.matches("October")) {
                    val22 = 10;
                } else if (val.matches("November")) {
                    val22 = 11;
                } else if (val.matches("December")) {
                    val22 = 12;
                }


                getDays();
                String qq = String.valueOf(numOfDays);
                totalFld.setText(qq);
                System.out.println(qq);

            } catch (Exception e) {
            }


        }
    }//GEN-LAST:event_gaAttTblMouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton7ActionPerformed
    
    // PAMBILANG NG DAYS PER MONTH excluding weekends
    public void getDays() {
        int schooldays = 0;
        Calendar cal = new GregorianCalendar();
        int month = val22;
        int year = cal.YEAR;
        int day = cal.DATE;
        cal.set(year, month, day);
        
        
        do {
            numOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            //cal.add(Calendar.DAY_OF_MONTH, 1);
            if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                schooldays++;
            }
        } while (cal.getTimeInMillis() < cal.getTimeInMillis());
        
        
    }
    
    
    public void ref(){
              int x = gaGradeTbl1.getSelectedColumn();
            while(x != 0) {
                x--;
            }
            int y = gaGradeTbl1.getSelectedRow();
            val = (String) gaGradeTbl1.getValueAt(y, x);
            try{ 
                
                String data[][] =   {{"January"," "," "," "},
                                    {"February"," "," "," "},
                                    {"March"," "," "," "},
                                    {"June"," "," "," "},
                                    {"July"," "," "," "},
                                    {"August"," "," "," "},
                                    {"September"," "," "," "},
                                    {"October"," "," "," "},
                                    {"November"," "," "," "},
                                    {"December"," "," "," "},};
                
               
                 attendanceList1 = mi.getList4("attendanceManage2", val);
                 ArrayList<Attendance> uu1 = new ArrayList<Attendance>();
                 for(int x1 = 0; x1 < attendanceList1.size(); x1++) {
                 uu1.add((Attendance) attendanceList1.get(x1));
            }
                 DefaultTableModel dtm1 = new DefaultTableModel(data, new String[] {gaAttTbl.getColumnName(0), gaAttTbl.getColumnName(1), gaAttTbl.getColumnName(2), gaAttTbl.getColumnName(3)});
                 gaAttTbl.setModel(dtm1);   
                 
                for(int x1 = 0; x1 < uu1.size(); x1++) {    
                      
                    if(uu1.get(x1).getmonth().equals("January")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 0, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 0, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 0, 3);     
                    }else if(uu1.get(x1).getmonth().equals("February")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 1, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 1, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 1, 3); 
                   }else if(uu1.get(x1).getmonth().equals("March")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 2, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 2, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 2, 3); 
                   }else if(uu1.get(x1).getmonth().equals("June")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 3, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 3, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 3, 3); 
                   }else if(uu1.get(x1).getmonth().equals("July")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 4, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 4, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 4, 3); 
                   }else if(uu1.get(x1).getmonth().equals("August")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 5, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 5, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 5, 3); 
                   }else if(uu1.get(x1).getmonth().equals("September")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 6, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 6, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 6, 3); 
                   }else if(uu1.get(x1).getmonth().equals("October")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 7, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 7, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 7, 3); 
                   }else if(uu1.get(x1).getmonth().equals("November")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 8, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 8, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 8, 3); 
                   }else if(uu1.get(x1).getmonth().equals("December")){
                       dtm1.setValueAt(uu1.get(x1).getpre(), 9, 2);
                       dtm1.setValueAt(uu1.get(x1).getabs(), 9, 1);
                       dtm1.setValueAt(uu1.get(x1).gettardy(), 9, 3); 
                   }      
                   
                   
                 }
                
                
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, e.getMessage());
                System.out.println(e);
            }
  
           }
    
    public void updateTable() {
//        try {
//            
//            list = mi.getList("attendanceManage");
//            ArrayList<Attendance> ss = new ArrayList<Attendance>();
//            for(int x = 0; x < list.size(); x++) {
//                ss.add((Attendance) list.get(x));
//                
//            }
//
//            DefaultTableModel dtm = new DefaultTableModel(new String[] {gaGradeTbl1.getColumnName(0), gaGradeTbl1.getColumnName(1), gaGradeTbl1.getColumnName(2), gaGradeTbl1.getColumnName(3), gaGradeTbl1.getColumnName(4)}, 0);
//            gaGradeTbl1.setModel(dtm);
//            for(int x= 0; x < ss.size(); x++) {
//                dtm.insertRow(x, new Object[] {ss.get(x).getidno(),ss.get(x).getname(), ss.get(x).getclasslevel(), ss.get(x).getsection(),  ss.get(x).getsy()});
//            
//            }   
//        } catch(Exception e) {
//            e.printStackTrace();
//        }
    }
           
    public void search() {
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AttendanceS_Manage(mi, cUser).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JLabel bannerL;
    private javax.swing.JLabel dateL;
    private javax.swing.JButton editButton;
    private javax.swing.JTable gaAttTbl;
    private javax.swing.JTable gaGradeTbl1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane49;
    private javax.swing.JScrollPane jScrollPane66;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JLabel timeL;
    private javax.swing.JTextField totalFld;
    // End of variables declaration//GEN-END:variables
}
